<?php


namespace app\sports\team\service;
use think\Exception;
use app\common\HTTP;
use app\common\OSS;
use think\facade\Log;
use app\sports\team\model\TeamModel;
use app\sports\player\service\PlayerCaiji;
class TeamCaiji
{
    private $user = '';
    private  $secret ='';
    private $url = 'open.sportnanoapi.com/api/sports/football/team/detail';
    /*
     * @array 传入过来一个球队信息的数组
     * @return bool 成功返回true ,失败返回false
     */
    public function __construct()
    {
        $this->user = Env::get('nami.user');
        $this->secret = Env::get('nami.secret');
    }

    public function TeamAddAndUpdate(array $array)
    {
//        $array = [
//            [
//          "id"=>10015,//球队id
//          "name_zh"=>"巴塞罗那",//中文名称
//          "name_zht"=>"巴塞隆拿",//粤语名称
//          "name_en"=>"FC Barcelona",//英文名称
//          "logo"=>"d159737531127e403115903764cb5145.png",//logo,url前缀:http://cdn.sportnanoapi.com/football/team/
//          "matchevent_id"=>120,//赛事id
//            ],
//            [
//          "id"=>10015,//球队id
//          "name_zh"=>"巴塞罗那",//中文名称
//          "name_zht"=>"巴塞隆拿",//粤语名称
//          "name_en"=>"FC Barcelona",//英文名称
//          "logo"=>"d159737531127e403115903764cb5145.png",//logo,url前缀:http://cdn.sportnanoapi.com/football/team/
//          "matchevent_id"=>120,//赛事id
//            ],
//        ];
        try{

//          思路:
    //        1.获取传入过来的球队的纳米id,查询接口
    //        用于存储,赛事传入过来的纳米id
            $teamid=[];
    //        要插入球队的数据
            $insertdata = [];
    //       远程获取新的球队的数据
            $file_get_date=[];
    //        球员信息整理
            $playerinfo = [];
            foreach ($array as $item){
                $teamid[]=$item['id'];
            }
    //        2.查询全部球队的纳米id个数
            $teaminfo=TeamModel::getInstance()->getTeamIdInfo($teamid);
    //        判断数据库是否存在球队的信息,如果不等于,那么就有一个球队的信息不再数据库中,那么就需要去接口查询了
            if (count($teaminfo)!=count($teamid)){
    //            将两个纳米id进行对比,将两者之间的差集求出来,去请求接口
                $arraynew= array_diff($teamid,$teaminfo);
                foreach ($arraynew as $id){
                    $file_get_date[]= json_decode(HTTP::getInstance()->doGet($this->url."?user=$this->user&secret=$this->secret&id=$id"),true);
                }
            }
            foreach ($file_get_date as $v){
                $v['oss_logo'] = OSS::getInstance()->uploadStream('','http://cdn.sportnanoapi.com/football/team/'.$v['logo'],true);
                $insertdata[]=[
                    'nami_team_id' => $v['id'],
                    'name'         => $v['name_zh'],
                    'logo'         => $v['oss_logo'],
                    'manager'      => $v['manager'],
                       'create_time'  =>date('Y-m-d H:i:s',time()),
                       'update_time'  =>date('Y-m-d H:i:s',time()),
                ];
    //            解析好球员的数据
                $v['json_decode_player'] = json_decode($v['players'],true);
    //            组装数据
                foreach ($v['json_decode_player'] as $player_info){
                    $player_info['player_logo'] = OSS::getInstance()->uploadStream('','http://cdn.sportnanoapi.com/football/player/'.$player_info['logo'],true);
                    $playerinfo[]=[
                        'nami_team_id'   => $v['id'],
                        'nami_player_id' =>$player_info['id'],
                        'name'           =>$player_info['name_zh'],
                        'logo'           =>$player_info['player_logo'],
                       'create_time'  =>date('Y-m-d H:i:s',time()),
                       'update_time'  =>date('Y-m-d H:i:s',time()),
                    ];
                }
    //        调用球员service的处理的service,写入
                $playerresult=PlayerCaiji::getInstance()->InsertPlayer($playerinfo);
                if (!$playerresult) throw new Exception("球员信息入库失败");
            }
//            调用球队模型插入批量插入数据
            $teamresult = TeamModel::getInstance()->insertTeamAll($insertdata);
               if (!$teamresult) throw new Exception("球队信息入库失败");
               Log::log('info','球队信息采集入库成功');
        }catch(Exception $e){
             Log::error('球队采集错误:'.$e->getMessage());
            return [];
        }
    }

}